/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.kramerius.rukopisy.tools;

import cz.mzk.oai.metadataRepository.harvest.tei.svn.SignatureSysnoMapper;
import cz.mzk.oai.metadataRepository.repository.Repository;
import cz.mzk.oai.metadataRepository.repository.collections.MetadataCollection;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Martin Řehánek <rehan at mzk.cz>
 */
public class ItemsTransferBuilder {

    private static final Logger logger = Logger.getLogger(ItemsTransferBuilder.class.getName());
    private final Repository repository;
    private final SignatureSysnoMapper mapper;
    private final File resultDir;
    private final CollectionDeterminant determinant;

    public ItemsTransferBuilder(Repository rep, SignatureSysnoMapper mapper, TeiManager teiManager, File resultDir) {
        this.repository = rep;
        this.mapper = mapper;
        this.resultDir = resultDir;
        this.determinant = new CollectionDeterminant(repository, teiManager);
    }

    public void buildTransferFiles() {
        Set<String> signatures = mapper.allSignatures();
        for (String signature : signatures) {
            Manuscript mns = new Manuscript(signature, mapper.getIdentifier(signature));
            processManuscript(mns);
        }
        saveTransfers();
    }

    private void processManuscript(Manuscript mns) {
        try {
            MetadataCollection originalCollection = repository.selectCollection(mns.getId());
            MetadataCollection newCollection = determinant.chooseCollection(mns, originalCollection);
            if (!originalCollection.getName().equals(newCollection.getName())) {
                ItemTransfer transformation = ItemTransfer.getTransfer(originalCollection, newCollection);
                System.out.println("adding " + mns.getId() + " to " + originalCollection.getName() + "->" + newCollection.getName());
                transformation.addIdentifier(mns.getId());
            }
        } catch (Exception ex) {
            logger.log(Level.SEVERE, "Cannot process manuscript {0}: {1}", new Object[]{mns.getId(), ex.getMessage()});
        }
    }

    private void saveTransfers() {
        for (ItemTransfer transfer : ItemTransfer.getAllTransfers()) {
            PrintWriter out = null;
            try {
                out = new PrintWriter(buildFile(transfer));
                transfer.writeTo(out);
                out.close();
            } catch (FileNotFoundException ex) {
                logger.log(Level.SEVERE, null, ex);
            } finally {
                out.close();
            }
        }
    }

    private File buildFile(ItemTransfer transfer) {
        String fileName = resultDir.getAbsolutePath() + File.separator
                + transfer.getFrom().getName() + "2"
                + transfer.getTo().getName() + ".move";
        return new File(fileName);
    }
}
